﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class ClienteRepository : MasterDataAccess , IClienteRepository
    {
        public List<CLIENTE> GetAllFromCliente()
        {
            var query = from p in Context.CLIENTEs
                        select p;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Take(1000).ToList();
        }

        public List<CLIENTE> GetFromClienteByCriterio(string criterio)
        {
            if (criterio != "" )
            {
                var query = from p in Context.CLIENTEs
                        where p.NombreRazonSocial.ToUpper().Contains(criterio.ToUpper()) ||
                        p.DniRuc.ToUpper().Contains(criterio.ToUpper())
                        select p;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query1 = from p in Context.CLIENTEs
                        select p;
                ObjectContext.Refresh(RefreshMode.StoreWins, query1);

                return query1.Take(1000).ToList();
            }


        }

        public CLIENTE GetFromOneClienteByDniRuc(string DniRuc)
        {
            //return Context.CLIENTEs.Where(c => c.DniRuc.Equals(DniRuc)).SingleOrDefault();
            return Context.CLIENTEs.Where(c => c.DniRuc.Equals(DniRuc)).FirstOrDefault();
        }

        public void AddCliente(CLIENTE cliente)
        {
            Context.CLIENTEs.Add(cliente);
            Context.SaveChanges();
        }

        public void UpdateCliente(CLIENTE cliente)
        {
            Context.CLIENTEs.Attach(cliente);
            Context.Entry(cliente).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void DeleteCliente(int id)
        {
            var existe = Context.CLIENTEs.Where(p => p.Id.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                Context.CLIENTEs.Remove(existe);
                Context.SaveChanges();
            }
        }

        public CLIENTE GetFromClienteById(int id)
        {
         //   return Context.CLIENTEs.Find(id);
            var query = from c in Context.CLIENTEs
                        where c.Id.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidadDniRucAndRazonSocial(string dniRuc, string razonSocial, int id)
        {
            var query = from c in Context.CLIENTEs
                        where ( c.DniRuc.ToUpper().CompareTo(dniRuc.ToUpper()) == 0 || c.NombreRazonSocial.ToUpper().CompareTo(razonSocial.ToUpper()) == 0 )
                        && c.Id != id
                        select c;
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }
    }
}
